package com.zmn.oms.services.impl.perf;

import com.zmn.common.dto.VtDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.oms.model.entity.perf.PerfDimensionDistribute;
import com.zmn.oms.model.entity.perf.PerfDimensionDistributeQuery;
import com.zmn.oms.persistence.interfaces.perf.PerfDimensionDistributeDao;
import com.zmn.oms.services.interfaces.perf.PerfDimensionDistributeService;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * 派单绩效纬度(PerfDimensionDistribute)表服务实现类
 *
 * @author hankangshuai
 * @since 2019-09-23 14:11:44
 */
@Service("perfDimensionDistributeService")
public class PerfDimensionDistributeServiceImpl implements PerfDimensionDistributeService {
    @Resource
    private PerfDimensionDistributeDao perfDimensionDistributeDao;

    /**
     * 通过主键查询单条数据
     *
     * @param dimensionId 主键
     * @return 实例对象
     */
    @Override
    @ReadOnlyConnection
    public PerfDimensionDistribute findByKey(Integer dimensionId) {
        return this.perfDimensionDistributeDao.findByKey(dimensionId);
    }

    /**
     * 查询所有数据
     *
     * @return 对象列表
     */
    @Override
    @ReadOnlyConnection
    public List<PerfDimensionDistribute> listAll() {
        return this.perfDimensionDistributeDao.listAll();
    }

    /**
     * 分页条件查询
     *
     * @param perfDimensionDistributeQuery 查询对象
     * @return 对象列表
     */
    @Override
    @ReadOnlyConnection
    public List<PerfDimensionDistribute> listPageByQuery(PerfDimensionDistributeQuery perfDimensionDistributeQuery) {
        return this.perfDimensionDistributeDao.listPageByQuery(perfDimensionDistributeQuery);
    }

    /**
     * 条件查询
     *
     * @param perfDimensionDistributeQuery 查询对象
     * @return 对象列表
     */
    @Override
    public List<PerfDimensionDistribute> listByQuery(PerfDimensionDistributeQuery perfDimensionDistributeQuery) {
        return this.perfDimensionDistributeDao.listByQuery(perfDimensionDistributeQuery);
    }

    /**
     * 条件查询纬度Id列表
     *
     * @param perfDimensionDistributeQuery 查询对象
     * @return 纬度ID列表
     */
    @Override
    @ReadOnlyConnection
    public List<Integer> listDimensionIdByQuery(PerfDimensionDistributeQuery perfDimensionDistributeQuery) {
        return this.perfDimensionDistributeDao.listDimensionIdByQuery(perfDimensionDistributeQuery);
    }

    /**
     * 条件计数
     *
     * @param perfDimensionDistributeQuery 查询对象
     * @return 影响行数
     */
    @Override
    public int countByQuery(PerfDimensionDistributeQuery perfDimensionDistributeQuery) {
        return this.perfDimensionDistributeDao.countByQuery(perfDimensionDistributeQuery);
    }

    /**
     * 通过主键删除数据
     *
     * @param dimensionId 主键
     * @return 影响行数
     */
    @Override
    public int deleteByKey(Integer dimensionId) {
        return this.perfDimensionDistributeDao.deleteByKey(dimensionId);
    }

    /**
     * 批量插入数据忽略重复
     *
     * @param distributes 要插入的数据
     * @return 影响行数
     */
    @Override
    public int insertOnDuplicateIgnoreBatch(List<PerfDimensionDistribute> distributes) {
        // 分片阀值
        int sliceThreshold = 500;
        if (isEmpty(distributes)) {
            return 0;
        }
        Integer count;
        if (distributes.size() > sliceThreshold) {
            List<List<PerfDimensionDistribute>> partition = ListUtils.partition(distributes, (int) Math.ceil(distributes.size() / sliceThreshold));
            count = partition.parallelStream().mapToInt(value -> perfDimensionDistributeDao.insertOnDuplicateIgnoreBatch(value)).sum();
        } else {
            count = perfDimensionDistributeDao.insertOnDuplicateIgnoreBatch(distributes);
        }
        return Objects.nonNull(count) ? count.intValue() : 0;
    }

    /**
     * 根据平台标识查派单员
     *
     * @param plat 平台标识
     * @return 派单员Vt
     */
    @Override
    @ReadOnlyConnection
    public List<VtDTO> listDistributorByPlat(Integer plat) {
        return this.perfDimensionDistributeDao.listDistributorByPlat(plat);
    }
}